package com.jindy.module.AssetDeclared.service.impl;

import java.util.Date;
import java.util.List;

import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.utils.DateUtils;
import com.jindy.module.AssetCheck.domain.AssetCheck;
import com.jindy.module.AssetCheck.mapper.AssetCheckMapper;
import com.jindy.module.AssetDetails.domain.AssetDetails;
import com.jindy.module.AssetDetails.mapper.AssetDetailsMapper;
import com.jindy.module.AssetReceive.domain.AssetReceive;
import com.jindy.module.AssetReceive.mapper.AssetReceiveMapper;
import com.jindy.module.AssetRepair.domain.AssetRepair;
import com.jindy.module.AssetRepair.mapper.AssetRepairMapper;
import com.jindy.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jindy.module.AssetDeclared.mapper.AssetDeclaredMapper;
import com.jindy.module.AssetDeclared.domain.AssetDeclared;
import com.jindy.module.AssetDeclared.service.IAssetDeclaredService;

import javax.annotation.Resource;

/**
 * 资产报废Service业务层处理
 * 
 * @author jindy
 * @date 2023-04-25
 */
@Service
public class AssetDeclaredServiceImpl implements IAssetDeclaredService 
{
    @Resource
    private AssetDeclaredMapper assetDeclaredMapper;

    @Resource
    private AssetDetailsMapper assetDetailsMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private AssetReceiveMapper assetReceiveMapper;

    @Resource
    private AssetRepairMapper assetRepairMapper;

    @Resource
    private AssetCheckMapper assetCheckMapper;

    /**
     * 查询资产报废
     * 
     * @param id 资产报废主键
     * @return 资产报废
     */
    @Override
    public AssetDeclared selectAssetDeclaredById(Long id)
    {
        return assetDeclaredMapper.selectAssetDeclaredById(id);
    }

    /**
     * 查询资产报废列表
     * 
     * @param assetDeclared 资产报废
     * @return 资产报废
     */
    @Override
    public List<AssetDeclared> selectAssetDeclaredList(AssetDeclared assetDeclared)
    {
        return assetDeclaredMapper.selectAssetDeclaredList(assetDeclared);
    }

    /**
     * 新增资产报废
     * 
     * @param assetDeclared 资产报废
     * @return 结果
     */
    @Override
    public int insertAssetDeclared(AssetDeclared assetDeclared)
    {
        int flag=0;
        //资产领用报废入口
        if(assetDeclared.getDeclaredReceiveId() != null){
            //资产领用信息
            AssetReceive assetReceive=assetReceiveMapper.selectAssetReceiveById(assetDeclared.getDeclaredReceiveId());
            assetDeclared.setDeclaredAssetId(assetReceive.getReceiveAssetId());
            assetDeclared.setDeclaredAssetName(assetReceive.getReceiveAssetName());
            assetDeclared.setDeclaredAssetClassName(assetReceive.getReceiveClassName());
            assetDeclared.setAssetBrand(assetReceive.getReceiveAssetBrand());
            assetDeclared.setAssetModel(assetReceive.getReceiveAssetModel());
            assetDeclared.setDeclaredQuantity(assetReceive.getReceiveQuantity());
            //当前使用人信息
            SysUser sysUser=sysUserMapper.selectUserById(assetReceive.getReceiveUserId());
            assetDeclared.setDeclaredUserName(sysUser.getUserName());
            assetDeclared.setDeclaredUserDeptId(sysUser.getDeptId());
            assetDeclared.setDeclaredUserDeptName(sysUser.getDept().getDeptName());

            if(assetReceive.getIsReturn().equals("维修中")){
                AssetRepair assetRepair=assetRepairMapper.selectAssetRepairByAssetReceiveId(assetReceive.getId());
                assetRepair.setRepairStatus("已报废");
                assetRepairMapper.updateAssetRepair(assetRepair);
            }

            //修改领用表物品状态为已报废
            if(!assetReceive.getIsReturn().equals("已归还") && !assetReceive.getIsReturn().equals("已报废")){
                    assetReceive.setIsReturn("已报废");
                    assetReceiveMapper.updateAssetReceive(assetReceive);
            }
        }

        //资产明细报废入口
        else {
            if (assetDeclared.getDeclaredAssetId() != null){
                AssetDetails assetDetails = assetDetailsMapper.selectAssetDetailsById(assetDeclared.getDeclaredAssetId());
                //数量校验
                if(assetDeclared.getDeclaredQuantity() <= assetDetails.getAssetAvailQuantity()) {
                    assetDeclared.setDeclaredAssetName(assetDetails.getAssetName());
                    assetDeclared.setDeclaredAssetClassName(assetDetails.getClassName());
                    assetDeclared.setAssetBrand(assetDetails.getAssetBrand());
                    assetDeclared.setAssetModel(assetDetails.getAssetModel());

                    //修改资产明细表可用数量
                    assetDetails.setAssetAvailQuantity(assetDetails.getAssetAvailQuantity()-assetDeclared.getDeclaredQuantity());
                    assetDetailsMapper.updateAssetDetails(assetDetails);
                }
            }
        }

            //查询报废负责人信息
        if(assetDeclared.getDeclaredRespId() != null) {
            SysUser sysUser = sysUserMapper.selectUserById(assetDeclared.getDeclaredRespId());
            assetDeclared.setDeclaredRespName(sysUser.getUserName());
            assetDeclared.setDeclaredRespDeptId(sysUser.getDeptId());
            assetDeclared.setDeclaredRespDeptName(sysUser.getDept().getDeptName());
        }

        assetDeclared.setCreateTime(DateUtils.getNowDate());
        flag=assetDeclaredMapper.insertAssetDeclared(assetDeclared);

        //资产盘点表新增资产报废
        AssetCheck assetCheck=new AssetCheck();
        assetCheck.setAssetId(assetDeclared.getDeclaredAssetId());
        assetCheck.setAssetName(assetDeclared.getDeclaredAssetName());
        assetCheck.setOperateId(assetDeclared.getId());
        assetCheck.setOperateType("资产报废");
        assetCheck.setOperateTime(new Date());
        assetCheckMapper.insertAssetCheck(assetCheck);

        return flag;
    }

    /**
     * 修改资产报废
     * 
     * @param assetDeclared 资产报废
     * @return 结果
     */
    @Override
    public int updateAssetDeclared(AssetDeclared assetDeclared)
    {
        assetDeclared.setUpdateTime(DateUtils.getNowDate());
        return assetDeclaredMapper.updateAssetDeclared(assetDeclared);
    }

    /**
     * 批量删除资产报废
     * 
     * @param ids 需要删除的资产报废主键
     * @return 结果
     */
    @Override
    public int deleteAssetDeclaredByIds(Long[] ids)
    {
        return assetDeclaredMapper.deleteAssetDeclaredByIds(ids);
    }

    /**
     * 删除资产报废信息
     * 
     * @param id 资产报废主键
     * @return 结果
     */
    @Override
    public int deleteAssetDeclaredById(Long id)
    {
        return assetDeclaredMapper.deleteAssetDeclaredById(id);
    }
}
